package com.yanqu.road.server.manger.activity.dafuweng.building;


import com.yanqu.road.entity.activity.dafuweng.DFWConfig;
import com.yanqu.road.entity.activity.dafuweng.DFWEvent;
import com.yanqu.road.entity.enums.activity.dafuweng.eDaFuWengBuildingType;
import com.yanqu.road.entity.enums.activity.dafuweng.eDaFuWengEventType;
import com.yanqu.road.entity.enums.eItemId;
import com.yanqu.road.pb.monopoly.MonopolyProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.dafuweng.DaFuWengMgr;
import com.yanqu.road.server.manger.activity.dafuweng.event.BaseDFWEvent;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author wpj
 * @Date 2021/1/5
 */
public abstract class BaseDFWBuilding {
    eDaFuWengBuildingType buildingType;

    GamePlayer player;

    DFWConfig dfwConfig;

    List<MonopolyProto.EventResultMsg> eventResultList = new ArrayList<>();

    String unionUid;


    public BaseDFWBuilding(DFWConfig dfwConfig, eDaFuWengBuildingType buildingType, GamePlayer player) {
        this.buildingType = buildingType;
        this.player = player;
        this.dfwConfig = dfwConfig;
        this.unionUid = player.getUserInfo().getUnionUid();
    }

    public abstract void reach();

    public abstract void passRoad();

    public void doReach() {
        base();
        for (DFWEvent dfwEvent : DaFuWengMgr.getReachEvents(dfwConfig, buildingType)) {
            BaseDFWEvent event = BaseDFWEvent.getBaseEvent(this, dfwEvent);
            doEvent(event);
        }
    }

    public void doPass() {
        for (DFWEvent dfwEvent : DaFuWengMgr.getPassEvents(dfwConfig, buildingType)) {
            BaseDFWEvent event = BaseDFWEvent.getBaseEvent(this, dfwEvent);
            doEvent(event);
        }
    }

    public void doEvent(BaseDFWEvent event) {
        eDaFuWengEventType eventType = event.getDfwEvent().getEventType();
        if (StringUtils.isNullOrEmpty(unionUid) && eDaFuWengEventType.isUnionEvent(eventType)) {
            return; // 没有商会无法触发
        }
        event.doEvent();
        if (event.getDfwEvent().getEventType() == eDaFuWengEventType.Blacksmith) {
            // 铁匠升级事件没有用了，不用发给客户端
            return;
        }
        MonopolyProto.EventResultMsg.Builder builder = MonopolyProto.EventResultMsg.newBuilder();
        builder.setEventId(event.getDfwEvent().getEventId());
        builder.setIsReachReward(event.isUnionCollectReachReward());
        builder.setBuildingId(buildingType.getValue());
        builder.setReward(event.getReward());
        builder.setCollectLimit(!event.isCanDoCollectEvent());
        eventResultList.add(builder.build());
        // 宝箱事件加一下收集类触发奖励
        if (event.getDfwEvent().getEventType() == eDaFuWengEventType.Box) {
            Map<Integer, BigInteger> collectGoods = event.getBoxCollectReward().getGoods();
            for (Map.Entry<Integer, BigInteger> entry : collectGoods.entrySet()) {
                if (entry.getKey() == eItemId.DFW_COLLECT_TIE_QI.getValue()) {
                    continue; // 铁器的后面处理
                }
                int collectEventByGoodsId = DaFuWengMgr.getCollectEventByGoodsId(entry.getKey());
                int collectBuildingId = DaFuWengMgr.getBuildingIdByGoodsId(entry.getKey());
                MonopolyProto.EventResultMsg.Builder builder2 = MonopolyProto.EventResultMsg.newBuilder();
                builder2.setEventId(collectEventByGoodsId);
                builder2.setIsReachReward(true);
                builder2.setBuildingId(collectBuildingId);
                builder2.setShowCollectReward(false);
                eventResultList.add(builder2.build());
            }
        }
        // 其利断金升级
        if (event.getNewBattleSkillId() != 0) {
            MonopolyProto.EventResultMsg.Builder builder1 = MonopolyProto.EventResultMsg.newBuilder();
            builder1.setEventId(event.getNewBattleSkillId());
            builder1.setIsReachReward(true);
            builder1.setBuildingId(buildingType.getValue());
            eventResultList.add(builder1.build());
        }
    }

    public void base() {
        int baseEventId = dfwConfig.getDfwBuildingConfigMap().get(buildingType.getValue()).getBaseEvent();
        BaseDFWEvent.getBaseEvent(this, dfwConfig.getDfwEventMap().get(baseEventId));
        doEvent(BaseDFWEvent.getBaseEvent(this, dfwConfig.getDfwEventMap().get(baseEventId)));
    }


    public static BaseDFWBuilding getBuilding(DFWConfig dfwConfig, eDaFuWengBuildingType buildingType, GamePlayer player) {
        if (buildingType == eDaFuWengBuildingType.YuanLin) {
            return new DFWYuanLinBuilding(dfwConfig, buildingType, player);
        } else if (buildingType == eDaFuWengBuildingType.RanFang) {
            return new DFWRanFangBuilding(dfwConfig, buildingType, player);
        } else if (buildingType == eDaFuWengBuildingType.XiangLiaoFang) {
            return new DFWXiangLiaoFangBuilding(dfwConfig, buildingType, player);
        } else if (buildingType == eDaFuWengBuildingType.YueFang) {
            return new DFWYueFangBuilding(dfwConfig, buildingType, player);
        } else if (buildingType == eDaFuWengBuildingType.TieJiangPu) {
            return new DFWTieJiangPuBuilding(dfwConfig, buildingType, player);
        } else if (buildingType == eDaFuWengBuildingType.ShenMiJiGuan1 || buildingType == eDaFuWengBuildingType.ShenMiJiGuan2) {
            return new DFWShenMiJiGuanBuilding(dfwConfig, buildingType, player);
        } else if (buildingType == eDaFuWengBuildingType.ChaSi) {
            return new DFWChaSiBuilding(dfwConfig, buildingType, player);
        } else if (buildingType == eDaFuWengBuildingType.DangPu) {
            return new DFWDangPuBuilding(dfwConfig, buildingType, player);
        } else if (buildingType == eDaFuWengBuildingType.DuFang) {
            return new DFWDuFangBuilding(dfwConfig, buildingType, player);
        } else if (buildingType == eDaFuWengBuildingType.ZaHuoPu) {
            return new DFWZaHuoPuBuilding(dfwConfig, buildingType, player);
        } else if (buildingType == eDaFuWengBuildingType.MinFang) {
            return new DFWMinFangBuilding(dfwConfig, buildingType, player);
        } else if (buildingType == eDaFuWengBuildingType.YiZhan) {
            return new DFWYiZhanBuilding(dfwConfig, buildingType, player);
        } else if (buildingType == eDaFuWengBuildingType.SiMiao) {
            return new DFWSiMiaoBuilding(dfwConfig, buildingType, player);
        } else if (buildingType == eDaFuWengBuildingType.SuanGuaTan) {
            return new DFWSuanGuaTanBuilding(dfwConfig, buildingType, player);
        } else if (buildingType == eDaFuWengBuildingType.HuaFang) {
            return new DFWHuaFangBuilding(dfwConfig, buildingType, player);
        }
        return null;
    }

    public eDaFuWengBuildingType getBuildingType() {
        return buildingType;
    }

    public GamePlayer getPlayer() {
        return player;
    }

    public DFWConfig getDfwConfig() {
        return dfwConfig;
    }

    public String getUnionUid() {
        return unionUid;
    }

    public List<MonopolyProto.EventResultMsg> getEventResultList() {
        return eventResultList;
    }
}
